package main

import (
	"blog2021/configs"
	_ "blog2021/schedule"
	"blog2021/tools/logger"
	"flag"
	"path"

	"github.com/gin-gonic/gin"
	"github.com/sevlyar/go-daemon"
)

func deamon(run func()) {
	cntxt := &daemon.Context{
		PidFileName: "blog2021.pid",
		PidFilePerm: 0644,
		LogFileName: "blog2021.log",
		LogFilePerm: 0640,
		WorkDir:     "./",
		Umask:       027,
		Args:        []string{},
	}

	d, err := cntxt.Reborn()
	if err != nil {
		logger.Error("Unable to run: ", err)
	}
	if d != nil {
		return
	}
	defer cntxt.Release()

	logger.Info("- - - - - - - - - - - - - - -")
	logger.Info("daemon started")

	gin.SetMode(gin.ReleaseMode)
	run()
}

func main() {
	logger.Setting(path.Join(configs.LogDir(), "ningto.log"))
	logger.Info("main start")
	deamonFlag := flag.Bool("d", false, "deamon server")
	flag.Parse()

	if *deamonFlag {
		deamon(startServer)
	} else {
		startServer()
	}
}
